package com.cnhis.cloudhealth.clinical.clidoctor.clinicalreceptiontransfer.bo;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.clidoctor.clinicalreceptiontransfer.dao.ClinicalReceptionDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.GlobalVariable;
import com.cnhis.cloudhealth.commons.utils.Page;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

@Service
public class ClinicalReceptionBo
{
    @Resource
    private ClinicalReceptionDao clinicalReceptionDao;
    
    @Resource
    private GetNewId getNewId;
    
    public ModelVo waitingToReception(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            clinicalReceptionDao.waitingToReception(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo finishedReception(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            clinicalReceptionDao.finishedReception(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo cancelReception(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            clinicalReceptionDao.cancelReception(map);
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo receptionOrQuit(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Map HashMap = null;
        try
        {
            HashMap = clinicalReceptionDao.receptionOrQuit(map);
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setMap(HashMap);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo updateClinicalReception(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        try
        {
            clinicalReceptionDao.updateClinicalReception(map);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo selectClinicalOffice(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = clinicalReceptionDao.selectClinicalOffice(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo hasSettleDoctorAdvice(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = clinicalReceptionDao.hasSettleDoctorAdvice(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo visittimeWhetherValidity(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Map HashMap = null;
        try
        {
            HashMap = clinicalReceptionDao.visittimeWhetherValidity(map);
            mo.setMap(HashMap);
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo personalFinishedPatient(Map map, int currPage, int pageSize)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        Page pg = null;
        try
        {
            pg = clinicalReceptionDao.personalFinishedPatient(map, currPage, pageSize);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        
        mo.setList(pg.getList());
        mo.setResult(StaticKeys.OH_SUCCESS);
        mo.setTotal(pg.getTotalNumber());
        mo.setRows(pg.getList());
        return mo;
    }
    
    public ModelVo queryConsultingRoomByAdministrativeOffice(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = clinicalReceptionDao.queryConsultingRoomByAdministrativeOffice(map);
            
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    public ModelVo queryDoctorByAdministrativeOffice(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo();
        List list = null;
        try
        {
            list = clinicalReceptionDao.queryDoctorByAdministrativeOffice(map);
            
        }
        catch (Exception e)
        {
            mo.setResult(StaticKeys.OH_FALSE);
            return mo;
        }
        mo.setList(list);
        mo.setResult(StaticKeys.OH_SUCCESS);
        return mo;
    }
    
    // 已取消接诊或已接诊完成
    private boolean existsReception(Map map)
        throws Exception
    {
        return clinicalReceptionDao.existsReception(map) > 0;
    }
    
    // 已下达有医嘱
    private boolean existsAdvice(Map map)
        throws Exception
    {
        return clinicalReceptionDao.existsAdvice(map) > 0;
    }
    
    // 挂号记录已接诊或已退号
    private boolean existsExit(Map map)
        throws Exception
    {
        return clinicalReceptionDao.existsExit(map) > 0;
    }
    
    private boolean existsBCK01C(Map map)
        throws Exception
    {
        return clinicalReceptionDao.existsBCK01C(map) > 0;
    }
    
    @SuppressWarnings("unchecked")
    private Map preParams(Map map)
        throws Exception
    {
        String aDate = DateUtils.getDateString(new Date());
        // 挂号医师id
        Long aFlag = 0l;
        // 开单科室id
        Long aBCK01C = 0l;
        int aUpdate = 0;
        // 挂号类别
        Long BCB01 = 0l;
        aFlag = clinicalReceptionDao.getBCE01D(map);
        BCB01 = clinicalReceptionDao.getBCB01(map);
        if ("0".equals(map.get("lSign").toString()))
        {
            aBCK01C = clinicalReceptionDao.getBCK01C(map);
            map.put("aBCK01C", aBCK01C);
            if (existsBCK01C(map))
            {
                aUpdate = 1;
            }
        }
        map.put("aDate", aDate);
        map.put("aUpdate", aUpdate);
        map.put("BCB01", BCB01);
        map.put("aFlag", aFlag);
        
        return map;
    }
    
    private void updateVAA(Map map)
        throws Exception
    {
        // 变更病人就诊科室和就诊状态
        clinicalReceptionDao.updateVAA1(map);
        // 变更病人挂号执行状态 、执行时间、执行者医师、执行者号、医师id
        clinicalReceptionDao.updateVAC1(map);
        // 变更挂号分诊诊室当前人数-1
        clinicalReceptionDao.updateBCZ1(map);
        // 变更病人挂号队列状态为接诊 1
        clinicalReceptionDao.updateVAD1(map);
        // 变更病人费用明细信息
        clinicalReceptionDao.updateVAJ1(map);
        // 对应就诊号的费用不存在 添加
        if (clinicalReceptionDao.existsVAJ1(map) == 0)
        {
            clinicalReceptionDao.saveVAJ1(map);
        }
    }
    
    private void cancelVAA(Map map)
        throws Exception
    {
        // 修改病人挂号 就诊 分诊 费用 相关表参数
        clinicalReceptionDao.cancelVAA(map);
    }
    
    public ModelVo execClinicVAC1Update(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
        int sign = GlobalVariable.IntegerIsNull(map.get("lSign").toString());
        // if(sign==1&&!existsReception(map)){
        // mo.setResult(StaticKeys.OH_FALSE);
        // mo.setResultMsg("此病人已取消接诊或已接诊完成，请刷新重新操作");
        // return mo;
        // }
        // if(sign==1&&existsAdvice(map)){
        // mo.setResult(StaticKeys.OH_FALSE);
        // mo.setResultMsg("此病人已下达有医嘱，不能取消接诊");
        // return mo;
        // }
        // if(sign==0&&!existsExit(map)){
        // mo.setResult(StaticKeys.OH_FALSE);
        // mo.setResultMsg("此挂号记录已接诊或已退号，请刷新重新操作");
        // return mo;
        // }
        // 参数处理
        map = preParams(map);
        if (sign == 0)
        {// 接诊
            updateVAA(map);
        }
        else
        {// 取消接诊
            cancelVAA(map);
        }
        return mo;
        
    }
    
    private boolean existsVAC1(Map map)
        throws Exception
    {
        return clinicalReceptionDao.existsVAC1(map) > 0;
    }
    
    private void saveZAE1(Map map)
        throws Exception
    {
        map.put("newID", getNewId.nextId());
        clinicalReceptionDao.saveZAE1(map);
    }
    
    private void updateParams(Map map)
        throws Exception
    {
        clinicalReceptionDao.updateParams(map);
    }
    
    public ModelVo execClinicChangeBCK1(Map map)
        throws Exception
    {
        ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
//        if (existsVAC1(map))
//        {
//            mo.setResult(StaticKeys.OH_FALSE);
//            mo.setResultMsg("当前病人无需转诊");
//            return mo;
//        }
        Long BCB01 = clinicalReceptionDao.getBCB01(map);
        map.put("BCB01", BCB01);
        // 资料变更修改记录
        saveZAE1(map);
        // 变更转诊相关参数
        updateParams(map);
        return mo;
    }
}
